package com.live.dengdengtv.activity.my;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;

import com.live.dengdengtv.CustomTitleActivity;
import com.live.dengdengtv.R;
import com.live.dengdengtv.bean.AppInfo;
import com.live.dengdengtv.bean.SimpleInfoData;
import com.live.dengdengtv.kernel.utils.UIUtils;
import com.live.dengdengtv.net.NetTaskList;
import com.live.dengdengtv.net.callback.ProgressDialogCallBack;
import com.live.dengdengtv.utils.AppDialog;
import com.live.dengdengtv.utils.Notice;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.builder.PostFormBuilder;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by wyf on 2016/8/18.
 */
public class SubInfoActivity extends CustomTitleActivity implements View.OnClickListener{

    private EditText real_name_et;
    private EditText phone_et;
    private EditText verify_et;
    private EditText identity_et;
    private EditText description_et;
    private Button get_verify_bt;
    private ImageButton front_idcard_ib;
    private ImageButton person_idcard_ib;


    private File fileFront, filePeople;
    private static final int FRONT_PIC = 10;
    private static final int PEOPLE_WITHID_PIC = 20;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_submit_info,R.layout.title_custom_red);
        setDefaultTitle("提交资料认证");

        iniView();
    }

    private void iniView() {
        real_name_et = (EditText) findViewById(R.id.real_name_et);
        phone_et = (EditText) findViewById(R.id.phone_et);
        verify_et = (EditText) findViewById(R.id.verify_et);
        identity_et = (EditText) findViewById(R.id.identity_et);
        description_et = (EditText) findViewById(R.id.description_et);

        get_verify_bt = (Button) findViewById(R.id.get_verify_bt);
        front_idcard_ib = (ImageButton) findViewById(R.id.front_idcard_ib);
        person_idcard_ib = (ImageButton) findViewById(R.id.person_idcard_ib);

        get_verify_bt.setOnClickListener(this);
        front_idcard_ib.setOnClickListener(this);
        person_idcard_ib.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {

        if(v.getId() == R.id.confirm_btn){
            submitInfo();
        }else if(v.getId() == R.id.get_verify_bt){
            onVerifyCodeClick();
        }else if(v.getId() == R.id.front_idcard_ib){
            getPic(FRONT_PIC);
        }else if(v.getId() == R.id.person_idcard_ib){
            getPic(PEOPLE_WITHID_PIC);
        }
    }


    private void onVerifyCodeClick( ) {
        Map<String,String> params = new HashMap<String, String>();
        params.put("phone", phone_et.getText().toString().trim());
        params.put("code_type", "3");
        params.putAll(AppInfo.getHeadInfo());
        requestData(NetTaskList.getVerify, params,1);
    }

    private void getPic(int picType){
        Intent intent_album = new Intent("android.intent.action.GET_CONTENT");
        intent_album.setType("image/*");
        startActivityForResult(intent_album, picType);
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode != RESULT_OK) {
            return;
        }

        switch (requestCode) {
            case FRONT_PIC:
                String path1 = UIUtils.getPath(this, data.getData());
                if (null != path1){
                    fileFront = new File(path1);
                    front_idcard_ib.setImageURI(Uri.fromFile(fileFront));
                }
                break;
            case PEOPLE_WITHID_PIC:
                String path2 = UIUtils.getPath(this, data.getData());
                if (null != path2){
                    filePeople = new File(path2);
                    person_idcard_ib.setImageURI(Uri.fromFile(filePeople));
                }
                break;
        }
    }

    @Override
    public boolean onResponse(SimpleInfoData response, int id) {
        if(id == 1){
            if(response.result == 0){
                Toast.makeText(context,"发送成功!",Toast.LENGTH_SHORT).show();
            }
        }else if(id == 2){
            if(response.result == 0){
                AppDialog.showAlertDialog(this,"已经成功提交认证大概2~7个工作日内我们会给您通知");
            }
        }
        return false;
    }

    private void submitInfo() {
        String name = real_name_et.getText().toString().trim();
        String phone = phone_et.getText().toString().trim();
        String verifyCode = verify_et.getText().toString().trim();
        String identity = identity_et.getText().toString().trim();
        String description = description_et.getText().toString().trim();

        if (TextUtils.isEmpty(name)) {
            Notice.alert(context, "姓名不能为空!");
            return;
        }
        if (TextUtils.isEmpty(phone)) {
            Notice.alert(context, "手机号不能为空!");
            return;
        }
        if (TextUtils.isEmpty(verifyCode)) {
            Notice.alert(context, "验证码不能为空!");
            return;
        }
        if (TextUtils.isEmpty(identity)) {
            Notice.alert(context, "身份证不能为空!");
            return;
        }
        if (TextUtils.isEmpty(description)) {
            Notice.alert(context, "认证描述不能为空!");
            return;
        }

        if (fileFront == null) {
            Notice.alert(context, "身份证正面照不能为空!");
            return;
        }
        if (filePeople == null) {
            Notice.alert(context, "人与身份证合照不能为空!");
            return;
        }

        PostFormBuilder postFormBuilder = OkHttpUtils.post().url(NetTaskList.update_personalInfo)
                .addParams("token", AppInfo.TOKEN)
                .addParams("phone_type", AppInfo.PHONE_TYPE)
                .addParams("phone_system_version", AppInfo.getPhone_system_version())
                .addParams("phone_app_version", AppInfo.getPhone_app_version())
                .addParams("client_id", AppInfo.getClient_id())
                .addParams("user_id", AppInfo.loginInfo.getUserId())

                .addParams("phone", phone)
                .addParams("code", verifyCode)
                .addParams("code_of_idcard", identity)
                .addParams("true_name", name )
                .addParams("description", description )
                .addParams("authentication_type", "10" )//认证类型：1、微博认证；2、QQ认证；3、微信认证；10、资料认证。
                .addFile("idcard_positive_image", fileFront.getName(),fileFront)
                .addFile("idcard_with_person_image", filePeople.getName(),filePeople);

        postFormBuilder.id(2).tag(this.getClass().getName())
                .build()
                .execute(new ProgressDialogCallBack(this,this));
    }
}
